package com.shwread.android.qysw.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.shwread.android.qysw10000152.R;
import com.shwread.android.ui.widget.LoadingView;
import com.shwread.android.ui.widget.refresh.PullToRefreshScrollView;
import com.shwread.android.ui.widget.refresh.PullToRefreshUtil;
import com.shwread.android.ui.widget.refresh.base.PullToRefreshBase;
import com.shwread.android.ui.widget.refresh.base.PullToRefreshBase.OnRefreshListener;
import com.shwread.http.ActionHelp;
import com.shwread.http.base.Log;
import com.shwread.http.base.ObjectHttpResponseHandler;

import org.apache.http.Header;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import logic.bean.RechargeInfo;
import logic.util.MoneyUtil;
import logic.util.Utils;


/**
 * OrderMenuRechargeRecordActivity
 * 
 * @Description 充值记录界面
 * @Author zhangdong
 * @Date 2015年7月13日下午16:14:40
 */
public class OrderMenuRechargeRecordActivity
        extends
        BaseBusinessActivity
        implements
        OnClickListener
{
	private Context                 context;
	private LoadingView             loadingView;
	private Button                  btnBack;
	private TextView                tvTitle;
	private LinearLayout            llBody, llNone;
	private PullToRefreshScrollView psView;
	private ListView                listView;
	private List<RechargeInfo>      data        = new ArrayList<RechargeInfo>();
	private MyAdapter               adapter     = new MyAdapter();
	private int                     length      = 20;
	private long                    timeStamp   = 0;
	private boolean                 isHasMore   = true;
	private int                     getDataType = 0;                            //0 首次加载  1刷新  2加载更多
	                                                                             
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.om_my_recharge_record);
		this.context = this;
		initView();
		initLoadingView();
		setListener();
		initData();
	}
	
	private Handler handler = new Handler()
	                        {
		                        @Override
		                        public void handleMessage(Message msg) {
			                        switch (msg.what) {
			                        
										default:
											break;
									}
								}
	                        };
	
	private void setListener() {
		btnBack.setOnClickListener(this);
		psView.setOnRefreshListener(new OnRefreshListener<ScrollView>()
		{
			
			@Override
			public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
				if (psView.isHeaderShown())
				{
					refrush();
				}
				else if (psView.isFooterShown())
				{
					loadMore();
				}
			}
		});
	}
	
	/**
	 * 加载更多
	 */
	private void loadMore() {
		if (isHasMore)
		{
			getDataType = 2;
			timeStamp = data.get(data.size() - 1).getCreateTime();
			getRecordList();
			PullToRefreshUtil.initLabel(context, psView);
		}
		else
		{
			PullToRefreshUtil.setLoadLabel(psView, "没有更多了");
			psView.onRefreshComplete();
		}
	}
	
	/**
	 * 刷新页面数据
	 */
	private void refrush() {
		getDataType = 1;
		timeStamp = 0L;
		PullToRefreshUtil.initLabel(context, psView);
		getRecordList();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
	}
	
	private void initView() {
		tvTitle = (TextView) findViewById(R.id.common_header_name);
		tvTitle.setText("充值记录");
		btnBack = (Button) findViewById(R.id.common_header_back);
		btnBack.setVisibility(View.VISIBLE);
		
		llBody = (LinearLayout) findViewById(R.id.ll_om_my_recharge_record_body);
		psView = (PullToRefreshScrollView) findViewById(R.id.om_recharge_record_psv);
		listView = (ListView) findViewById(R.id.lv_om_my_recharge_record);
		
		llNone = (LinearLayout) findViewById(R.id.ll_om_my_recharge_record_none);
		
		loadingView = (LoadingView) findViewById(R.id.order_loading_view);
	}
	
	private void initLoadingView() {
		loadingView.setLoadingAnim(R.anim.home_progress);
		loadingView.changeLoadingStatus(0);
		loadingView.setVisibility(View.VISIBLE);
		llBody.setVisibility(View.GONE);
		llNone.setVisibility(View.GONE);
	}
	
	private void initData() {
		listView.setAdapter(adapter);
		getRecordList();
	}
	
	public void getRecordList() {
		ActionHelp.qryBalanceRechargeOrderList(context, timeStamp, length, new QryRecordHandler());
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.common_header_back:
				this.finish();
				break;
			default:
				break;
		}
	}
	
	private class MyAdapter
	        extends
	        BaseAdapter
	{
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return data.size();
		}
		
		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return null;
		}
		
		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null)
			{
				holder = new ViewHolder();
				convertView = View.inflate(context, R.layout.om_my_recharge_record_list_item, null);
				holder.tvMoney = (TextView) convertView.findViewById(R.id.tv_om_my_recharge_record_list_item_money);
				holder.tvTime = (TextView) convertView.findViewById(R.id.tv_om_my_recharge_record_list_item_time);
				holder.tvStatus = (TextView) convertView.findViewById(R.id.tv_om_my_recharge_record_list_item_status);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvMoney.setTextColor(Color.parseColor("#ff9600"));
			holder.tvMoney.setText("¥" + Utils.getMoneyFormat(MoneyUtil.divide(data.get(position).getTotalMoney(), 100)));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			holder.tvTime.setText(sdf.format(new Date(data.get(position).getCreateTime())));
			//	        if(data.get(position).getStatus() == 0) {
			//	        	holder.tvStatus.setText("充值中");
			//	        	holder.tvStatus.setTextColor(Color.parseColor("#323232"));
			//	        } else 
			if (data.get(position).getStatus() == 1)
			{
				holder.tvStatus.setText("充值成功");
				//	        	holder.tvStatus.setTextColor(Color.parseColor("#ff9600"));
			}
			else
			{
				holder.tvStatus.setText("充值失败");
				//	        	holder.tvStatus.setTextColor(Color.parseColor("#c6c6c6"));
			}
			return convertView;
		}
		
	}
	
	private class ViewHolder
	{
		private TextView tvMoney, tvTime, tvStatus;
	}
	
	private class QryRecordHandler
	        extends
	        ObjectHttpResponseHandler<List<RechargeInfo>>
	{
		@Override
		public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
			Log.i(responseString);
			if (statusCode < 1000)
			{
				Utils.ToastShow(context, "网络错误");
			}
			else
			{
				Utils.ToastShow(context, "请求出错");
			}
			if (getDataType != 0)
			{
				psView.onRefreshComplete();
			}
		}
		
		@Override
		public void onSuccess(int statusCode, Header[] headers, List<RechargeInfo> object, String responseString) {
			Log.i(responseString);
			loadingView.setVisibility(View.GONE);
			if (getDataType != 0)
			{
				psView.onRefreshComplete();
			}
			if (object == null || object.size() == 0)
			{
				isHasMore = false;
				if (getDataType != 2)
				{
					llBody.setVisibility(View.GONE);
					llNone.setVisibility(View.VISIBLE);
				}
			}
			else
			{
				if (object.size() < length)
				{
					isHasMore = false;
				}
				else
				{
					isHasMore = true;
				}
				if (getDataType != 2)
				{
					data.clear();
				}
				llBody.setVisibility(View.VISIBLE);
				llNone.setVisibility(View.GONE);
				data.addAll(object);
				adapter.notifyDataSetChanged();
				Utils.setListViewHeightBasedOnChildren(listView, adapter);
			}
		}
		
		@Override
		public Type getType() {
			return new TypeToken<List<RechargeInfo>>()
			{
			}.getType();
		}
	}
	
	@Override
	public Handler getUIHandler() {
		return handler;
	}
	
}
